import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import UserRuleTable from '../components/sideBar/UserRuleTable'
import Log from '../components/sideBar/Log'
import AccessTypeTable from '../components/sideBar/AccessTypeTable'
import AdminTable from '../components/sideBar/AdminTable'
import FlowLimitTable from '../components/sideBar/FlowLimitTable'
import UserDetailsTable from '../components/sideBar/UserDetailsTable'
import Overview from '../components/sideBar/Overview'
Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  { path: '/', redirect: '/login' },
  { path: '/home',
    redirect: '/welcome',
    component: Home,
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/UserRuleTable', component: UserRuleTable },
      { path: '/Log', component: Log },
      { path: '/AccessTypeTable', component: AccessTypeTable },
      { path: '/FlowLimitTable', component: FlowLimitTable },
      { path: '/UserDetailsTable', component: UserDetailsTable },
      { path: '/Overview', component: Overview },
      { path: '/AdminTable', component: AdminTable }
    ] }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  /*
  * to 将要访问的路径
  * from 代表从哪个路由跳转而来
  * next 是一个函数，表示放行
  * next() 放行 next('/login')强制跳转
  * */
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token，强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})
export default router
